<template>
  <div>
    <!--<div v-if="!isAddShow" class="user_list">-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="用户昵称"
        style="width: 100px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.importance"
        placeholder="用户权限"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in importanceOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>

      <!--<el-select v-model="listQuery.type"-->
      <!--placeholder="Type" clearable-->
      <!--class="filter-item" style="width: 130px">-->
      <!--<el-option v-for="item in calendarTypeOptions"-->
      <!--:key="item.key"-->
      <!--:label="item.display_name+'('+item.key+')'"-->
      <!--:value="item.key"/>-->
      <!--</el-select>-->

      <el-select
        v-model="listQuery.sort"
        style="width: 140px"
        class="filter-item"
      >
        <el-option
          v-for="item in sortOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新增商品
      </el-button>

      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
      >
        导出
      </el-button>

      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-upload2"
        @click="handleDownload"
      >
        导入
      </el-button>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="userId" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.userId }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户昵称"
        prop="username"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户密码"
        prop="password"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.password }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户权限"
        prop="role"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.role }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户姓名"
        prop="name"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户性别"
        prop="gender"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.gender === 1 ? "男":"女" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户头像"
        prop="pic"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <img :src="scope.row.pic" width="70px" height="80px" alt="加载中">
        </template>
      </el-table-column>

      <el-table-column
        label="用户创建日期"
        prop="createDate"
        sortable="custom"
        align="center"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
          >
            修改
          </el-button>
          <el-button
            v-if="row.status!=='deleted'"
            size="mini"
            type="danger"
            @click="handleDel(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      small
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width: 400px;
               margin-left:50px;"
      >

        <el-form-item label="用户昵称" prop="username">
          <el-input v-model="temp.username" />
        </el-form-item>

        <el-form-item label="用户密码" prop="password">
          <el-input v-model="temp.password" />
        </el-form-item>

        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>

        <el-form-item label="用户权限" prop="role">
          <el-input v-model="temp.role" />
        </el-form-item>

        <el-form-item label="用户性别" prop="gender">
          <el-input v-model="temp.gender" />
        </el-form-item>

        <el-form-item label="用户头像" prop="pic">
          <vue-ueditor-wrap
            v-model="temp.pic"
            :config="myConfig"
          />
        </el-form-item>

        <el-form-item label="用户创建日期" prop="createDate">
          <el-input v-model="temp.createDate" type="date" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >
          确定
        </el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import ajax from '@/utils/baseurl'
import Pagination from '@/components/Pagination'
import VueUeditorWrap from 'vue-ueditor-wrap'

var p
export default {
  name: 'Users',
  directive: { waves },
  components: { Pagination, VueUeditorWrap },
  data() {
    return {
      myConfig: {
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: 240,
        // 初始容器宽度
        initialFrameWidth: '100%',
        // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
        serverUrl: 'http://localhost:8080/upload',
        // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
        UEDITOR_HOME_URL: '/UEditor/'
      },
      isAddShow: false,
      total: 0,
      tableKey: 0,
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      temp: {
        username: '',
        password: '',
        role: '',
        pic: '',
        gender: '',
        createDate: '',
        name: ''
      },
      rules: {
        username: [{ required: true, message: '用户昵称不能为空!', trigger: 'change' }],
        password: [{ required: true, message: '登录密码不能为空!', trigger: 'change' }],
        name: [{ required: true, message: '用户姓名不能为空!', trigger: 'change' }]
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      listQuery: {
        page: 1,
        limit: 10,
        importance: '',
        title: '',
        sort: '+id'
      },
      importanceOptions: ['管理员', 'vip用户', '普通用户'],
      // calendarTypeOptions: [
      //   {key: 'CN', display_name: 'China'},
      //   {key: 'US', display_name: 'USA'},
      //   {key: 'JP', display_name: 'Japan'},
      //   {key: 'EU', display_name: 'Eurozone'}
      // ],
      sortOptions: [{ label: 'ID 升序', key: '+id' },
        { label: 'ID 降序', key: '-id' }]
    }
  },
  created() {
    p = this
    this.getList()
  },
  methods: {
    updateData() {
      this.createData()
    },
    resetTemp() {
      this.temp = {
        username: '',
        password: '',
        role: '',
        pic: '',
        gender: '',
        createDate: '',
        name: ''
      }
    },
    createData() {
      var p = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ajax(p.dialogStatus === 'create' ? 'addUsers.html' : 'updateUsers.html', this.temp, function(data) {
            if (data) {
              p.dialogFormVisible = false
              p.getList()
              p.$notify({
                title: '搞定',
                message: p.dialogStatus === 'create' ? '添加成功' : '修改成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleCreate() {
      this.isAddShow = true
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listLoading = true
      ajax('getUsersByPage.html', p.listQuery, function(data) {
        data = JSON.parse(data)
        p.list = data.page.list
        p.total = data.page.totalCount
        p.listLoading = false
      })
    },
    handleDel(row) {
      var p = this
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ajax('delUsersById.html', { 'id': row.userId }, function(data) {
          if (data) {
            p.getList()
            p.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList() {
      this.listLoading = true
      ajax('getUsersByPage.html', p.listQuery, function(data) {
        data = JSON.parse(data)
        p.list = data.page.list
        p.total = data.page.totalCount
        p.listLoading = false
      })
    }
  }
}
</script>
